// GicScreen.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "GicScreen.h"
#include "DispatchMsg.h"
#include "WinUtil.h"

#define MAX_LOADSTRING 100

#define ICON_NOIFY_MESSAGE (WM_USER+100)

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name


// Global Variables, local
HBITMAP _hBitMap;
DCUtil copyDC;

HWND hDlg;

BOOL _bLeftBtDown;

DCUtil::EDGE_POS _curPos;

POINT pointA;

HCURSOR _hCursorNS;
HCURSOR _hCursorWE;
HCURSOR _hCursorALL;


// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);

// global variables definition here
//
//
//


int APIENTRY _tWinMain(HINSTANCE hInstance,
					   HINSTANCE hPrevInstance,
					   LPTSTR    lpCmdLine,
					   int       nCmdShow)
{
	_bLeftBtDown = FALSE;

	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_GICSCREEN, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GICSCREEN));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}


void RePaintBMP(HWND hWnd, HBITMAP hBitMap, DCUtil& copyDC, POINT pa, POINT pb)
{

}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	// Note: we should add CS_DBLCLKS property to whidow style
	//		 if NOT, this window will not received double click message
	//
	wcex.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;

	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GICSCREEN));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL; //MAKEINTRESOURCE(IDC_GICSCREEN);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;

	RECT WndRect;
	HWND hwnd = GetDesktopWindow();
	::GetWindowRect(hwnd,&WndRect);		

	hInst = hInstance; // Store instance handle in our global variable
	hWnd = CreateWindowEx(0,
		szTitle,
		szTitle,
		WS_POPUP | WS_EX_WINDOWEDGE ,
		100,
		100,
		300,
		200,
		0,
		0,
		hInstance,
		NULL);

	if (!hWnd)
	{
		return FALSE;
	}

	if(FALSE == ::RegisterHotKey(hWnd,
		GIC_HOTKEY_NOTIFY_ACTION_CODE,
		MOD_CONTROL | MOD_ALT,
		'A'))
	{	
		::MessageBox(hWnd, 			 
			_TEXT("Error register w+a+a"),
			_TEXT("Error"),
			MB_OK | MB_ICONERROR);

		return FALSE;
	}	


	if(AddNotificationIcon(hInstance, hWnd, ICON_NOIFY_MESSAGE, 
		LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SMALL)), szTrayIconTips) == FALSE)
	{
		::MessageBox(hWnd, 			 
			_TEXT("Error add notification icon"),
			_TEXT("Error"),
			MB_OK | MB_ICONERROR);
		return FALSE;
	}

	_hCursorNS = LoadCursor(NULL, IDC_SIZENS);
	_hCursorWE = LoadCursor(NULL, IDC_SIZEWE);
	_hCursorALL = LoadCursor(NULL, IDC_SIZEALL);


	ShowWindow(hWnd, SW_HIDE);	
	UpdateWindow(hWnd);

	return TRUE;
}



BOOL DispatchNotifyMsg(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(lParam)
	{
	case WM_LBUTTONDOWN:
		{			
			break;
		}

	case WM_RBUTTONDOWN:
		{
			// Create a content menu here
			//
			//
			HMENU hContextMenu = ::LoadMenu(hInst, MAKEINTRESOURCE(IDC_GICSCREEN));
			POINT cursorPoint;
			::GetCursorPos(&cursorPoint);

			HMENU hMenu;            // top-level menu 
			HMENU hmenuTrackPopup;  // shortcut menu 

			// Load the menu resource.
			hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDC_GICSCREEN));
			if (hMenu == NULL) 
				return FALSE; 

			// TrackPopupMenu cannot display the menu bar so get 
			// a handle to the first shortcut menu. 
			hmenuTrackPopup = ::GetSubMenu(hMenu, 0);
			// Display the shortcut menu. Track the right mouse 
			// button. 

			// Using the first submenu to set the position of the whole menu
			// Windows will adjust the position for you
			//
			TrackPopupMenu(hmenuTrackPopup, 
				TPM_LEFTALIGN | TPM_RIGHTBUTTON, 
				cursorPoint.x, cursorPoint.y, 0, hWnd, NULL); 

			DestroyMenu(hMenu); 
			break;

		}

	default:
		break;
	}
	return TRUE;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	switch (message)
	{	
	case WM_PAINT:
		{
			HDC hdc = BeginPaint(hWnd, &ps);
			RePaintBMP(hWnd, _hBitMap, copyDC);			
			EndPaint(hWnd, &ps);
			break;
		}


	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	case WM_QUIT:
		DeleteNotificationIcon();
		break;

	case WM_LBUTTONDBLCLK:		
	case WM_LBUTTONDOWN:
	case WM_LBUTTONUP:
	case WM_MOUSEMOVE:
	case WM_RBUTTONDOWN:
	case WM_RBUTTONUP:
		DispatchMouseMsg(hWnd, message, wParam, lParam);
		break;

	case WM_HOTKEY:
		DispatchHotKeyMsg(hWnd, message, wParam, lParam);	
		break;

	case WM_KEYDOWN:
	case WM_KEYUP:
		DispatchKeyMsg(hWnd, message, wParam, lParam);
		break;

	case ICON_NOIFY_MESSAGE:
		DispatchNotifyMsg(hWnd, message, wParam, lParam);
		break;

	case WM_COMMAND:
		DispatchContextMenuMsg(hWnd, message, wParam, lParam);
		break;

	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}